package com.atguigu.service.impl;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.util.CastUtil;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseDao houseDao;

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }

    @Override
    public PageInfo<House> findPage(Map<String, Object> filters) {
        // 页码数
        int pageNum = CastUtil.castInt(filters.get("pageNum"),1);
        // 每页记录数
        int pageSize = CastUtil.castInt(filters.get("pageSize"),10);
        // 开启分页
        PageHelper.startPage(pageNum,pageSize);
        // 设置各种id对应的名字
        Page<House> page = getEntityDao().findPage(filters);
        /*List<House> result = page.getResult();
        for (House house : result) {
            house.setHouseTypeName(dictDao.getNameById(house.getHouseTypeId()));
            house.setFloorName(dictDao.getNameById(house.getFloorId()));
            house.setBuildStructureName(dictDao.getNameById(house.getBuildStructureId()));
            house.setDirectionName(dictDao.getNameById(house.getDirectionId()));
            house.setDecorationName(dictDao.getNameById(house.getDecorationId()));
            house.setHouseUseName(dictDao.getNameById(house.getHouseUseId()));
        }*/

        // 开启查询
        return new PageInfo<House>(page,10);
    }

    @Override
    public void publish(Long id, int status) {
        // 根据id修改房子的status
        House house =new House();
        house.setId(id);
        house.setStatus(status);
        houseDao.update(house);
    }

    @Override
    public PageInfo<HouseVo> findListPage(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {
        PageHelper.startPage(pageNum,pageSize);

        Page<HouseVo> page =houseDao.findListPage(houseQueryVo);
        for (HouseVo houseVo : page) {
            // 户型
            houseVo.setHouseTypeName(dictDao.getNameById(houseVo.getHouseTypeId()));
            // 楼层
            houseVo.setFloorName(dictDao.getNameById(houseVo.getFloorId()));
            // 朝向
            houseVo.setDirectionName(dictDao.getNameById(houseVo.getDirectionId()));
        }


        return new PageInfo<>(page,10);
    }

    // 重写getById方法

    @Override
    public House getById(Serializable id) {
        House house = houseDao.getById(id);
        house.setHouseTypeName(dictDao.getNameById(house.getHouseTypeId()));
        house.setFloorName(dictDao.getNameById(house.getFloorId()));
        house.setBuildStructureName(dictDao.getNameById(house.getBuildStructureId()));
        house.setDirectionName(dictDao.getNameById(house.getDirectionId()));
        house.setDecorationName(dictDao.getNameById(house.getDecorationId()));
        house.setHouseUseName(dictDao.getNameById(house.getHouseUseId()));
        return house;
    }
}
